package com.inspire.springboot.controller;

import com.inspire.springboot.service.IJCTestService;
import com.inspire.springboot.service.imp.IJCTestServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

@RestController
@RequestMapping("ijc")
public class IJCTestController {
    public final Logger logger = LoggerFactory.getLogger(MQController.class);

    ThreadPoolExecutor COMMON_THREAD_POOL = new ThreadPoolExecutor(1,
            1,
            60,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    ExecutorService executorService1 = Executors.newFixedThreadPool(5);
    /*
    异常测试
     */
    @RequestMapping("ijcTest1")
    public String ijcTest1(){

        IJCTestService ijcTestService=new IJCTestServiceImpl();
        ijcTestService.test01();
        ijcTestService.test02();

        return "ijcTest1";
    }

    /*
    测试循环
     */
    @RequestMapping("ijcTest2")
    public String ijcTest2(){
        IJCTestService ijcTestService=new IJCTestServiceImpl();
        for(int i=0;i<3;i++){
            ijcTestService.fun1();
            for(int j=0;j<4;j++){
                ijcTestService.test03();
                if(j==3){
                    ijcTestService.test04();
                }
            }
        }

        ijcTestService.fun1();
        System.out.println("end!");
        return "ijcTest2";
    }

    /*
    循环带异常demo测试打印异常时带次数
     */
    @RequestMapping("ijcTest3")
    public String ijcTest3(){
        IJCTestService ijcTestService=new IJCTestServiceImpl();
        for(int i=0;i<3;i++){
            ijcTestService.fun1();
            for(int j=0;j<4;j++){
                ijcTestService.test03();
                if(j==3){
                    ijcTestService.test02();
                }
            }
        }

        ijcTestService.fun1();
        System.out.println("end!");
        return "ijcTest2";
    }

    /*
    多线程测试
     */
    @RequestMapping("ijcTest4/{id}")
    public String ijcTest4(@PathVariable("id") int id){
        IJCTestService ijcTestService=new IJCTestServiceImpl();
        ijcTestService.test03();
        ijcTestService.fun1();

        final ThreadLocal<Integer> threadLocal=new InheritableThreadLocal<Integer>();
        threadLocal.set(100);
        COMMON_THREAD_POOL.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("COMMON_THREAD_POOL threadLocal.get() = " + threadLocal.get());
            }
        });
        threadLocal.set(101);
        scheduledExecutorService.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("scheduledExecutorService threadLocal.get() = " + threadLocal.get());
            }
        });
        threadLocal.set(102);
        executorService1.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("executorService1 threadLocal.get() = " + threadLocal.get());
            }
        });
        logger.info("多线程测试end!");
        return "ijcTest4";
    }
}
